﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
namespace UcAsp.Net.BLL
{
    public class AttachementFileFilter
    {
        private static string path = AppDomain.CurrentDomain.BaseDirectory;
        BLL.AttachementFileBLL fileBll = new BLL.AttachementFileBLL();
        public AttachementFileFilter(string url, Uri urlreferrer)
        {
            if (path.Substring(path.Length - 1, 1) != "\\")
            {
                path = path + "\\";
            }
            if(url.ToLower().IndexOf(".png")>-1||url.ToLower().IndexOf(".gif")>-1||url.ToLower().IndexOf(".jpg")>-1)
            {
                Thread thread = new Thread(new ParameterizedThreadStart(Record));
                string referrer = string.Empty;
                if (urlreferrer != null) {
                    referrer = urlreferrer.AbsoluteUri;
                }
                thread.Start(new string[] { url, referrer });
            }
            
           
        }
        public void Record(object url)
        {
            try {
                string[] obj = (string[])url;
                string md5 = Comm.FileMD5.GetMD5HashFromFile(path + obj[0]);
                Model.AttachementFile atfile = fileBll.GetFileByMd5(md5);
                if (atfile != null)
                {
                    atfile.VisitTime = DateTime.Now;                    
                    fileBll.Upate(atfile);
                }
            }
            catch { }
            
        }
    }
}
